package cn.edu.ecnu.gis.client.view;

import java.util.List;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.*;
import cn.edu.ecnu.gis.client.MashupGISEntryPoint;
import cn.edu.ecnu.gis.client.model.Gadget;
import cn.edu.ecnu.gis.client.model.GadgetClass;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.TextBox;

public class DesktopView extends Composite implements DockableListener {

    private TabPanel gadgetTabs = new TabPanel();
    private final DesktopViewListener listener;
    private DockPanel outer = new DockPanel();
    private FlowPanel businessGadgetsMenu = new FlowPanel();
    private FlowPanel geoprocessingGadgetsMenu = new FlowPanel();
    private FlowPanel searchGadgetsMenu = new FlowPanel();
    private FlowPanel uploadGadgetsMenu = new FlowPanel();
    private FlowPanel gisGadgetMenu = new FlowPanel();
    private ScrollPanel pageScroller;
    private TopPanel topPanel = new TopPanel();

    public DesktopView(DesktopViewListener ls) {
        this.listener = ls;
        initWidget(outer);
        outer.setWidth("100%");
        outer.setSpacing(4);
        outer.setBorderWidth(1);

        StackPanel shutcut = new StackPanel();

        Window.enableScrolling(false);
        Window.setMargin("10px");
        // shutcutScroller = new ScrollPanel(shutcut);
        // shutcutScroller.setSize("100%", "100%");

        businessGadgetsMenu.setStyleName("GadgetsMenu");
        businessGadgetsMenu.setWidth("100%");
        businessGadgetsMenu.setHeight("100%");

        gisGadgetMenu.setStyleName("GadgetsMenu");
        gisGadgetMenu.setWidth("100%");
        gisGadgetMenu.setHeight("100%");

        geoprocessingGadgetsMenu.setStyleName("GadgetsMenu");
        geoprocessingGadgetsMenu.setWidth("100%");
        geoprocessingGadgetsMenu.setHeight("100%");

        uploadGadgetsMenu.setStyleName("GadgetsMenu");
        uploadGadgetsMenu.setWidth("100%");
        uploadGadgetsMenu.setHeight("100%");
        HorizontalPanel uploadBox = new HorizontalPanel();
        uploadBox.add(new TextBox());
        uploadBox.add(new Button("上传"));
        uploadGadgetsMenu.add(uploadBox);

        searchGadgetsMenu.setStyleName("GadgetsMenu");
        searchGadgetsMenu.setWidth("100%");
        searchGadgetsMenu.setHeight("100%");


        HorizontalPanel searchBox = new HorizontalPanel();
        searchBox.add(new TextBox());
        searchBox.add(new Button("搜索"));
        searchGadgetsMenu.add(searchBox);

        shutcut.add(searchGadgetsMenu, "微件搜索");
        shutcut.add(gisGadgetMenu, "地图微件");
        shutcut.add(businessGadgetsMenu, "业务微件");
        shutcut.add(geoprocessingGadgetsMenu, "分析微件");
        shutcut.add(uploadGadgetsMenu, "微件上传");
        shutcut.showStack(0);


        outer.add(shutcut, DockPanel.WEST);
        // outer.add(topPanel, DockPanel.NORTH);
        //add gadgets
        List classes = GadgetClass.getClasses();
        //businessGadgetsMenu.add(new Label("Add Gadget: "));
        for (int i = 0; i < classes.size(); ++i) {
            final GadgetClass gadgetClass = (GadgetClass) classes.get(i);

            Hyperlink gadgetLink = new Hyperlink(gadgetClass.getName(), "");

            //gadgetLink.setHTML(imageItemHTML(gadgetClass.newGadget().getBoundImage(), ""));
            gadgetLink.addClickListener(new ClickListener() {

                public void onClick(Widget sender) {
                    insertGadget(gadgetClass.newGadget());
                }
            });
            switch (gadgetClass.getGadgetType()) {
                case 0:
                    businessGadgetsMenu.add(gadgetLink);
                    break;
                case 1:
                    gisGadgetMenu.add(gadgetLink);
                    break;
                case 2:
                    geoprocessingGadgetsMenu.add(gadgetLink);
                    break;
            }

        }

        //create the tab panel
        pageScroller = new ScrollPanel(gadgetTabs);

        pageScroller.setSize("100%", "100%");
        outer.add(pageScroller, DockPanel.CENTER);
        //outer.add(gadgetTabs, DockPanel.CENTER);
        outer.setCellWidth(pageScroller, "100%");
        outer.setCellHeight(pageScroller, "100%");
        gadgetTabs.setWidth("100%");
        //gadgetTabs.setHeight("100%");

        //create the tabs and pages
        gadgetTabs.add(createPage(), "<div>页面1</div>", true);
        gadgetTabs.add(createPage(), "<div>页面2</div>", true);

        //select the first tab 
        gadgetTabs.selectTab(0);
        outer.setWidth("100%");
    }

    private String imageItemHTML(AbstractImagePrototype imageProto, String title) {
        return "<span>" + imageProto.getHTML() + title + "</span>";
    }

//    public DockPanel createPage() {
//        DockPanel page = new DockPanel();
//        page.setStyleName("GadgetPage");
//        page.setWidth("100%");
//        page.setHeight("100%");
//        page.setSpacing(4);
////        page.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
////        page.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
//        createColumn(page, DockPanel.NORTH,"100","30");
//        createColumn(page, DockPanel.EAST,"20","40");
//        createColumn(page, DockPanel.WEST,"20","40");
//                createColumn(page, DockPanel.SOUTH,"100","30");
//        createColumn(page, DockPanel.CENTER,"60","40");
//        return page;
//    }
//
//    public void createColumn(DockPanel page, DockPanel.DockLayoutConstant direction, String width, String height) {
//        FlowPanel column = new FlowPanel();
//        column.setStyleName("GadgetColumn");
//        page.add(column, direction);
//        page.setCellWidth(column, width);
//        page.setCellHeight(column, height);
//    }
    public HorizontalPanel createPage() {
        HorizontalPanel page = new HorizontalPanel();
        page.setStyleName("GadgetPage");
        page.setWidth("100%");
        page.setHeight("100%");
        page.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
        createColumn(page, 0);
        createColumn(page, 1);
        createColumn(page, 2);
        return page;
    }

    public void createColumn(HorizontalPanel page, int columnNumber) {
        //create the column with a FlowPanel 
        FlowPanel column = new FlowPanel();
        column.setStyleName("GadgetColumn");
        page.add(column);
        page.setCellWidth(column, "33%");
        page.setCellHeight(column, "100%");
        column.setHeight("100%");
        DockableWidget.addContainer(column);
    }
//    public DockPanel getCurrentPage() {
//        return (DockPanel) gadgetTabs.getDeckPanel().getWidget(gadgetTabs.getDeckPanel().getVisibleWidget());
//    }

    public HorizontalPanel getCurrentPage() {
        return (HorizontalPanel) gadgetTabs.getDeckPanel().getWidget(gadgetTabs.getDeckPanel().getVisibleWidget());
    }

    public void insertGadget(Gadget gadget) {
        FlowPanel column = (FlowPanel) getCurrentPage().getWidget(0);
        insertGadget(column, gadget);
        MashupGISEntryPoint.widgetsList.put(gadget.getTitle(), gadget);
        gadget.setGadgetListener(listener);
        listener.onInterfaceChange();
    }

    protected void insertGadget(FlowPanel column, Gadget gadget) {
        GadgetContainerView gadgetContainer = new GadgetContainerView(gadget);
        DockableWidget dw = new DockableWidget(gadgetContainer);
        column.add(dw);
        dw.addDockableListener(this);
    }

    public void setLayoutFromString(String layout) {
        List classes = GadgetClass.getClasses();
        Document document = XMLParser.parse(layout);
        Element element = document.getDocumentElement();
        NodeList pages = element.getElementsByTagName("Page");
        for (int i = 0; i < pages.getLength(); i++) {
            HorizontalPanel page = (HorizontalPanel) gadgetTabs.getDeckPanel().getWidget(i);
            //DockPanel page = (DockPanel) gadgetTabs.getDeckPanel().getWidget(i);
            Element pageElement = (Element) pages.item(i);
            NodeList columns = pageElement.getElementsByTagName("Column");
            for (int j = 0; j < columns.getLength(); j++) {
                FlowPanel column = (FlowPanel) page.getWidget(j);
                Element columnElement = (Element) columns.item(j);
                NodeList gadgets = columnElement.getElementsByTagName("Gadget");
                for (int k = 0; k < gadgets.getLength(); ++k) {
                    Element gadgetElement = (Element) gadgets.item(k);
                    String name = gadgetElement.getAttribute("name");
                    for (int l = 0; l < classes.size(); ++l) {
                        GadgetClass gc = (GadgetClass) classes.get(l);
                        if (name.compareTo(gc.getName()) == 0) {
                            Gadget gadget = gc.newGadget();
                            gadget.fromXML(gadgetElement);
                            insertGadget(column, gadget);
                            MashupGISEntryPoint.widgetsList.put(gadget.getTitle(), gadget);
                            l = classes.size();
                        }
                    }
                }
            }
        }
    }

    public String getLayoutAsString() {
        Document document = XMLParser.createDocument();
        Element element = document.createElement("Desktop");
        document.appendChild(element);
        int pageCount = gadgetTabs.getDeckPanel().getWidgetCount();
        for (int i = 0; i < pageCount; ++i) {
            Element pageElement = element.getOwnerDocument().createElement("Page");
            element.appendChild(pageElement);
            HorizontalPanel page = (HorizontalPanel) gadgetTabs.getDeckPanel().getWidget(i);
            // DockPanel page = (DockPanel) gadgetTabs.getDeckPanel().getWidget(i);
            int columnCount = page.getWidgetCount();
            for (int j = 0; j < columnCount; j++) {
                FlowPanel column = (FlowPanel) page.getWidget(j);
                Element columnElement = element.getOwnerDocument().createElement("Column");
                pageElement.appendChild(columnElement);
                int gadgetCount = column.getWidgetCount();
                for (int k = 0; k < gadgetCount; ++k) {
                    Element gadgetElement = element.getOwnerDocument().createElement("Gadget");
                    columnElement.appendChild(gadgetElement);
                    DockableWidget dockacbleWidget = (DockableWidget) column.getWidget(k);
                    GadgetContainerView gadgetContainer = (GadgetContainerView) dockacbleWidget.getWidget();
                    gadgetContainer.getGadget().toXML(gadgetElement);
                }
            }
        }
        return document.toString();
    }

    public void onDocked(Widget widget) {
        listener.onInterfaceChange();

    }
}
